package cn.zane.designPattern.createPatterns.abstractFactoryPatterns.design.factory;

import cn.zane.designPattern.createPatterns.abstractFactoryPatterns.design.workshop.ICacheAdapter;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @Author: zane
 * @Date: 2024/12/29
 * @Description: 使用代理类来构建一个 抽象工厂
 */
public class JDKProxyFactory {

    public static <T> T getProxy(Class<T> cacheClazz, Class<? extends ICacheAdapter> cacheAdapter) throws InstantiationException, IllegalAccessException {
        InvocationHandler handler = new JDKInvocationHandler(cacheAdapter.newInstance());
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        return (T) Proxy.newProxyInstance(classLoader, new Class[]{cacheClazz}, handler);
    }
}
